package java3.io;

public class B21client {

    private static short game = 0;
    public static boolean esDebug = false;
    public static boolean esRobot = false;
    public static int sec;

    public static void procesaInput(final byte[] paramArrayOfByte, final int paramInt) {
        try {
            switch (game) {
                case 0:
                    break;
                case 1:
                    Runnable run = new Runnable() {
                        public void run() {
                            switch (paramInt) {
                                case 51:
                                    Tuerto.playGame(paramArrayOfByte);
                                    break;
                                case 46:
                                case 50:
                                    Tuerto.finishGame();
                                    break;
                                case 42:
                                    Tuerto.initGame(new byte[]{0, 0});
                                    break;
                            }
                        }
                    };
                    Thread hilo = new Thread(run);
                    hilo.start();
                    break;
                case 2:
                    Runnable run2 = new Runnable() {
                        public void run() {
                            switch (paramInt) {
                                case 51:
                                    ChessPlayer.playGame(paramArrayOfByte);
                                    break;
                                case 46:
                                case 50:
                                    ChessPlayer.finishGame();
                                    break;
                                case 42:
                                    ChessPlayer.initGame(paramArrayOfByte);
                                    break;
                            }
                        }
                    };
                    Thread hilo2 = new Thread(run2);
                    hilo2.start();
                    break;
                case 3:
                    Runnable run3 = new Runnable() {
                        public void run() {
                            switch (paramInt) {
                                case 51:
                                    Comepiezas.playGame(paramArrayOfByte);
                                    break;
                                case 46:
                                case 50:
                                    Comepiezas.finishGame();
                                    break;
                                case 42:
                                    Comepiezas.initGame(paramArrayOfByte);
                                    break;
                            }
                        }
                    };
                    Thread hilo3 = new Thread(run3);
                    hilo3.start();
                    break;
            }
        } catch (Exception localException) {
            System.out.println("**ERROR INPUT: " + localException);
            localException.printStackTrace();
        }
    }

    public static void procesaOutput(final byte[] paramArrayOfByte) {
        if ((paramArrayOfByte.length == 8) || (paramArrayOfByte.length == 9)) {
            if ((paramArrayOfByte[0] == 0)
                    && ((paramArrayOfByte[1] == 6) || (paramArrayOfByte[1] == 7))
                    && (paramArrayOfByte[2] == 24)
                    && ((paramArrayOfByte[3] == 0) || (paramArrayOfByte[3] == 1))
                    && ((paramArrayOfByte[4] == 2) || (paramArrayOfByte[4] == 3))
                    && (paramArrayOfByte[5] == 0)
                    && (paramArrayOfByte[6] == 47)) {
                int i = paramArrayOfByte[7];

                String str = "null";

                switch (i) {
                    case 48:
                        game = 0;
                        break;
                    case 49:
                        game = 1;
                        str = "tuerto";
                        break;
                    case 50:
                        game = 2;
                        str = "jester";
                        break;
                    case 51:
                        game = 3;
                        str = "comepiezas";
                        break;
                    case 52:
                        game = 4;
                        str = "pasapiezas";
                        break;
                    case 53:
                        game = 5;
                        str = "atomic";
                        break;
                    case 100:
                        esDebug = !esDebug;
                }

                if (paramArrayOfByte.length == 9) {
                    sec = paramArrayOfByte[8] - 48;
                } else {
                    sec = 1;
                }
                System.out.println("***" + str + "***:" + "Robot: " + esRobot + (esDebug ? "(d)" : ""));
                //System.out.println(game + ", sec: " + sec);
                //System.out.println("Robot: " + esRobot);
                //System.out.println("Debug: " + esDebug);
                //System.out.println("**************************");
            }
        }

        if (game != 0 && paramArrayOfByte.length > 2) {
            if ((paramArrayOfByte[0] == 0) && (paramArrayOfByte[1] == 8) && (paramArrayOfByte[2] == 51)) {
                if (game == 1) {
                    Runnable run = new Runnable() {
                        public void run() {
                            Tuerto.playGame(new byte[]{-1, paramArrayOfByte[4], paramArrayOfByte[3]});
                        }
                    };
                    Thread hilo = new Thread(run);
                    hilo.start();
                } else if (game != 5) {
                    if (game == 3) {
                        if (B21Util.isEnroquecorto() || B21Util.isEnroquelargo()) {
                            int ficha = B21Util.obtenerPosicion(-1)[0][paramArrayOfByte[3]];

                            if (ficha == 6) {
                                B21Util.setEnroquecorto(false);
                                B21Util.setEnroquelargo(false);
                            } else if (ficha == 4) {
                                switch (paramArrayOfByte[3]) {
                                    case 0:
                                    case 56:
                                        B21Util.setEnroquelargo(false);
                                        break;
                                    case 7:
                                    case 63:
                                        B21Util.setEnroquecorto(false);
                                        break;
                                }
                            }
                        } 
                        
                        if (B21Util.isEnroquecortoRival() || B21Util.isEnroquelargoRival()) {
                            if (B21Util.obtenerPosicion(-1)[0][paramArrayOfByte[4]] == 4) {
                                switch (paramArrayOfByte[4]) {
                                    case 0:
                                    case 56:
                                        B21Util.setEnroquelargoRival(false);
                                        break;
                                    case 7:
                                    case 63:
                                        B21Util.setEnroquecortoRival(false);
                                        break;
                                }
                            }
                        }
                    }

                    B21Util.ingresarJugada(paramArrayOfByte[3], paramArrayOfByte[4], paramArrayOfByte[5]);
                }

            } else if ((paramArrayOfByte.length == 3)
                    && (paramArrayOfByte[0] == 0) && (paramArrayOfByte[1] == 1) && (paramArrayOfByte[2] == 10)) {
                esRobot = !esRobot;
                System.out.println("-robot-" + esRobot);
            }
        }
    }
}